package com.demo.shiroDemo10.service.impl;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.demo.shiroDemo10.enums.EUserTypeInUserPerm;
import com.demo.shiroDemo10.service.ShiroService;
import com.demo.shiroDemos.mapper.OperatorMapper;
import com.demo.shiroDemos.mapper.OperatorRoleMapper;
import com.demo.shiroDemos.mapper.UserPermMapper;
import com.demo.shiroDemos.model.Operator;
import com.demo.shiroDemos.model.OperatorExample;
import com.demo.shiroDemos.model.OperatorRole;
import com.demo.shiroDemos.model.OperatorRoleExample;
import com.demo.shiroDemos.model.UserPerm;
import com.demo.shiroDemos.model.UserPermExample;
import com.google.common.base.Function;
import com.google.common.collect.Collections2;

@Service
public class ShiroServiceImpl implements ShiroService {

	@Autowired
	private OperatorMapper operatorMapper;
	@Autowired
	private OperatorRoleMapper operatorRoleMapper;
	@Autowired
	private UserPermMapper userPermMapper;
	
	@Override
	public Operator getOperatorByUsername(String username) {
		Operator operator = null;
				
		OperatorExample example = new OperatorExample();
		example.or().andNameEqualTo(username);
		List<Operator> list = operatorMapper.selectByExample(example);
		if(list.size() == 1){
			operator = list.get(0);
		}
		
		return operator;
	}

	@Override
	public Set<String> getRolesByUserName(String username) {
		Set<String> ret = null;
		
		OperatorRoleExample example = new OperatorRoleExample();
		example.or().andOperatorNameEqualTo(username);
		List<OperatorRole> list = operatorRoleMapper.selectByExample(example);
		if (CollectionUtils.isNotEmpty(list)) {
			ret = new HashSet<>();
			Collection<String> roles = Collections2.transform(list, new Function<OperatorRole, String>(){
				@Override
				public String apply(OperatorRole input) {
					return input.getRoleName();
				}
			});
			ret.addAll(roles);
		}
		
		return ret;
	}

	@Override
	public Set<String> getPermsByUserNameAndRoles(String username, Set<String> roleNames) {
		Set<String> ret = null;
		
		List<UserPerm> list = new ArrayList<>();
		UserPermExample example = new UserPermExample();
		if (StringUtils.isNoneEmpty(username)) {
			example.or().andUserTypeEqualTo(EUserTypeInUserPerm.USER.getValue()).andUserNameEqualTo(username);
			list.addAll(userPermMapper.selectByExample(example));
		}
		if (CollectionUtils.isNotEmpty(roleNames)) {
			List<String> roleNameList = new ArrayList<>();
			roleNameList.addAll(roleNames);
			example.clear();
			example.or().andUserTypeEqualTo(EUserTypeInUserPerm.ROLE.getValue()).andUserNameIn(roleNameList);
			list.addAll(userPermMapper.selectByExample(example));		
		}
		if (CollectionUtils.isNotEmpty(list)) {
			ret = new HashSet<>();
			Collection<String> perms = Collections2.transform(list, new Function<UserPerm, String>(){
				@Override
				public String apply(UserPerm input) {
					return input.getPermName();
				}
			});
			ret.addAll(perms);
		}
		
		return ret;
	}

}
